<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@taglib prefix="mvc" uri="http://www.springframework.org/tags/form" %>
<%@taglib prefix="fmt" uri="http://www.springframework.org/tags" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>研究领域分组</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=10,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
    <link rel="stylesheet" type="text/css" href="/lib/layui/layui/css/layui.css"/>
    <script type="text/javascript" src="/js/news/jquery-1.9.1.js"></script>
    <script src="/js/base/base.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../js/jquery/jquery.cookie.js"></script>
    <script src="/lib/jQuery-File-Upload-master/jquery.ui.widget.js" type="text/javascript" charset="utf-8"></script>
    <script src="/lib/jQuery-File-Upload-master/jquery.iframe-transport.js" type="text/javascript" charset="utf-8"></script>
    <script src="/lib/jQuery-File-Upload-master/jquery.fileupload.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript" src="/js/common/fileupload.js"></script>
    <script type="text/javascript" src="/lib/layui/layui/layui.js"></script>
    <script type="text/javascript" src="/lib/layui/layui/layui.all.js"></script>
</head>
<style>
    .mbox {
        padding: 8px
    }

    .item img {
        height: 40px;
    }


    .btn{
        line-height: 30px;
    }

    .btn button {
        float: right;
    }

    .layui-form-item .layui-input-inline{
        width: 110px;
    }
    .cont div{
        text-align: center;
        margin-bottom: 15px;
    }
    .layui-form-checkbox[lay-skin=primary] i{
        width: 18px;
        height: 18px;
        margin-top: 6px;
    }
    .addTian{
        display: inline-block;
        position: absolute;
        right: 10px;
        top: 6px;
    }

</style>
<body>
<div class="mbox">
    <div class="item" style="position: relative">
        <img src="../img/yeji.png" alt="" style="margin: 0 5px 0 20px;">
        <span style="font-size: 22px;display: inline-block;vertical-align: middle;">研究领域分组</span>
    </div>
    <hr class="layui-bg-blue">
    <div>
        <div class="layui-form">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <input type="radio" lay-filter="ctreateType" name="ctreateType" value="1" title="项目申报" checked>
                    <input type="radio" lay-filter="ctreateType" name="ctreateType" value="2" title="结题任务" >
                    <input type="radio" lay-filter="ctreateType" name="ctreateType" value="4" title="单项评选" >
                </div>
            </div>
        </div>

        <table class="layui-hide" id="test" lay-filter="test"></table>
    </div>

</div>
</body>
</html>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-sm" lay-event="enter">进入</a>
</script>
<script>

    //新增分组 或者  编辑
    function addGroup(planId,edit){
        layer.open({
            type: 1,
            offset: '50px',
            type: 1,
            title: "添加",
            btn: ['确定', '取消'],
            area: ['30%', '30%'],
            content: '<div id="cont" style="margin: 10px">\n' +
                '        <form class="layui-form" action="" id="ajaxforms" lay-filter="ajaxforms" >\n' +
                '            <div class="layui-form-item" style="margin-top: 15px">\n' +
                '                <div class="layui-inline">\n' +
                '                    <label class="layui-form-label">编号:</label>\n' +
                '                    <div class="layui-input-block">\n' +
                '                        <input style="width: 196px;" type="text" name="planNameM"  autocomplete="off" class="layui-input">\n' +
                '                    </div>\n' +
                '                </div>\n' +
                '            </div>\n' +
                '            <div class="layui-form-item" style="margin-top: 15px">\n' +
                '                <div class="layui-inline">\n' +
                '                    <label class="layui-form-label">名称:</label>\n' +
                '                    <div class="layui-input-block">\n' +
                '                        <input style="width: 196px;" type="text" name="planNameF"  autocomplete="off" class="layui-input">\n' +
                '                    </div>\n' +
                '                </div>\n' +
                '            </div>\n' +
                '        </form>\n' +
                '    </div>',
            success: function () {

                if (edit != 0){
                    $.ajax({
                        url:'/RpmResearchGrouping/findResearchGrouping',
                        dataType: 'json',
                        type: 'get',
                        data:{
                            planId:planId,
                            research:edit
                        },
                        success:function (res) {
                            if (res.flag){
                                $('input[name="planNameM"]').val(res.obj[0].research);
                                $('input[name="planNameF"]').val(res.obj[0].researchName);
                            }
                        }
                    })
                }

            },
            yes:function (index) {
                var research = $('input[name="planNameM"]').val();
                var researchName = $('input[name="planNameF"]').val();

                $.post('/RpmResearchGrouping/editOrInsertResearchGrouping',{
                    planId: planId,
                    research: research,
                    researchName: researchName,
                    topicIds:'',
                    epUserids:'',
                    remarks:''
                },function(res){
                    if (res.flag){
                        layer.close(index);

                        //渲染第二实例
                        layui.table.reload('test2');
                    }
                });
                return false;

            }
        })

    }

    //删除分组
    function delGroup(rfgId){
        layer.confirm('是否确定删除?', {icon: 3, title:'提示',offset: '40%'}, function(index){
            $.ajax({
                url:'/RpmResearchGrouping/delRfgId',
                dataType: 'json',
                type: 'get',
                data:{
                    rfgId:rfgId
                },
                success:function (res) {
                    if (res.flag){
                        layer.close(index);

                        //渲染第二实例
                        layui.table.reload('test2');
                    }
                }
            })
        });
    }

    //添加项目
    function TopicSelected(planId,research){
        layer.open({
            type: 1,
            title: '添加',
            btn: ['确定', '取消'],
            shade: 0.5,
            area: ['80%', '90%'],
            offset: '50px',
            content:'<div class="right">\n' +
                '<form id="search1" class="layui-form" lay-filter="formTest" style="margin: 10px auto; width: 80%">' +
                '   <div class="layui-form-item">\n' +
                '    <div class="layui-inline">\n' +
                '      <label class="layui-form-label">学段</label>\n' +
                '      <div class="layui-input-inline" style="width: 190px;">\n' +
                '        <select name="School">\n' +
                '          <option value="">请选择 学段</option>\n' +
                '        </select>\n' +
                '      </div>\n' +
                '    </div>\n' +
                '    <div class="layui-inline">\n' +
                '      <label class="layui-form-label">项目类别</label>\n' +
                '      <div class="layui-input-inline" style="width: 190px;">\n' +
                '        <select name="category" >\n' +
                '          <option value="">请选择 项目类别</option>\n' +
                '        </select>\n' +
                '      </div>\n' +
                '    </div>\n' +
                '    <div class="layui-inline">\n' +
                '      <label class="layui-form-label">研究领域</label>\n' +
                '      <div class="layui-input-inline" style="width: 190px;">\n' +
                '        <select name="RESEARCH" >\n' +
                '          <option value="">请选择 研究领域</option>\n' +
                '        </select>\n' +
                '      </div>\n' +
                '    </div>\n' +
                '    <div class="layui-inline">\n' +
                '            <button type="button" class="layui-btn layui-btn-sm" lay-submit lay-filter="searchLook" planid = "' + planId + '">搜索</button>' +
                '    </div>' +
                '  </div>' +
                '</form>' +
                '    <div id="transTable"></div>\n' +
                '</div>',
            success:function (res) {

                var ctreateType = $('input[name="ctreateType"]:checked').val();
                if (ctreateType == '4'){
                    $('#search1').hide();
                }else{
                    //学段接口
                    $.get('/RpmDictonary/selectDictionaryByNo',{
                        dictNo : 'SECTION_TYPE'
                    },function(res1){
                        var data1 = res1.data;
                        var str1 = '';
                        for (var x in data1){
                            str1 += '<option value="' + data1[x].dictNo + '">' + data1[x].dictName + '</option>';
                        }
                        $('select[name="School"]').append(str1);
                        layui.form.render('select');
                        layui.form.val("formTest", {
                            "School": research
                        });
                    });

                    //项目类别
                    $.get('/RpmDictonary/selectDictionaryByNo',{
                        dictNo : 'PROJECT_TYPE'
                    },function(res2){
                        var data2 = res2.data;
                        var str2 = '';
                        for (var c in data2){
                            str2 += '<option value="' + data2[c].dictNo + '">' + data2[c].dictName + '</option>';
                        }
                        $('select[name="category"]').append(str2);
                        layui.form.render('select');
                    });

                    //研究领域
                    $.get('/RpmDictonary/selectDictionaryByNo',{
                        dictNo : 'RESEARCH_TYPE'
                    },function(res3){
                        var data3 = res3.data;
                        var str3 = '';
                        for (var v in data3){
                            str3 += '<option value="' + data3[v].dictNo + '">' + data3[v].dictName + '</option>';
                        }
                        $('select[name="RESEARCH"]').append(str3);
                        layui.form.render('select');
                    });
                }

                //查询项目数据
                transTable1(planId,research,'','');
            },
            yes:function (index) {
                var tableleft = layui.table.cache["left-table-1"]
                var ids = '';
                for(var i=0;i<tableleft.length;i++){
                    ids += tableleft[i].topicId + ',';
                }

                $.ajax({
                    url:'/RpmResearchGrouping/editOrInsertResearchGrouping',
                    dataType: 'json',
                    type: 'get',
                    data:{
                        planId:planId,
                        research:research,
                        topicIds:ids,
                    },
                    success:function (res) {
                        layer.close(index);

                        //渲染第二实例
                        layui.table.reload('test2');
                    }
                });

            }
        })
    }

    //添加专家
    function ExpertSelected(planId,research){
        $.ajax({
            url:'/RpmExpert/getRpmExpertSelected',
            dataType: 'json',
            type: 'get',
            data:{
                planId:planId,
                research:research
            },
            success:function (res) {
                var datas = res.object
                var datas1 =[]
                var datas2 = []

                for(var i = 0;i<datas.length;i++){
                    if(datas[i].isselected==0){
                        datas1.push(datas[i])
                    }else{
                        datas2.push(datas[i])
                    }
                }

                layer.open({
                    type: 1,
                    title: '专家分配',
                    btn: ['确定', '取消'],
                    shade: 0.5,
                    area: ['80%', '90%'],
                    offset: '50px',
                    content:'<div class="right">\n' +
                        '    <div id="transTable"></div>\n' +
                        '</div>',
                    success:function (res) {
                        layui.transfer.render({
                            elem: "#transTable",
                            cols: [
                                { type: 'checkbox', fixed: 'left' }
                                , { field: 'name', title: '专家名称', align: 'center'}
                                , { field: 'unit', title: '单位', align: 'center'}
                                , { field: 'telNoHome', title: '研究专长', align: 'center'}
                            ],
                            data: [ datas2 , datas1],
                            tabConfig: { 'page': true, 'limit': 10, 'limits': [20, 50, 100], 'height': 600 }
                        })
                    },
                    yes:function (index) {
                        var tableleft = layui.table.cache["left-table-1"];
                        var ids = ''
                        for(var i=0;i<tableleft.length;i++){
                            ids += tableleft[i].expertId+','
                        }
                        $.ajax({
                            url:'/RpmResearchGrouping/editOrInsertResearchGrouping',
                            dataType: 'json',
                            type: 'get',
                            data:{
                                planId:planId,
                                research:research,
                                epUserids:ids,
                            },
                            success:function (res) {
                                layer.close(index);

                                //渲染第二实例
                                layui.table.reload('test2');
                            }
                        })
                    }
                })
            }
        })
    }

    //渲染项目的穿梭框
    function transTable1(planId,research,projectType,researchType){
        $.ajax({
            url:'/rpmTopic/getRpmTopicSelected',
            dataType: 'json',
            type: 'get',
            data:{
                planId:planId,
                research:research,
                projectType:projectType,
                researchType:researchType,
            },
            success:function (res) {
                var datas = res.obj;
                var datas1 =[];
                var datas2 = [];
                for(var i = 0;i<datas.length;i++){
                    if( datas[i].isselected == 0 ){
                        datas1.push(datas[i])
                    }else{
                        datas2.push(datas[i])
                    }
                }

                //渲染穿梭框
                layui.transfer.render({
                    elem: "#transTable",//指定元素
                    cols: [
                        { type: 'checkbox', fixed: 'left' }
                        , { field: 'topicName', title: '项目名称', align: 'center',}
                        , { field: 'researchName', title: '研究领域', align: 'center'}
                        , { field: 'topicTypeName', title: '类别', align: 'center' }
                    ],
                    data: [datas2, datas1],
                    tabConfig: { 'page': true, 'limit': 10, 'limits': [20, 50, 100], 'height': 600 },
                })
            }
        })
    }

    //判断返回是否为空
    function empty(esName) {
        if(esName != undefined){
            return esName;
        }
        return '';
    }

    layui.config({
        base: '/lib/layui/layui/lay/modules/'
    }).use(['table', 'form', 'laydate','upload','transfer','layer'], function () {
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var transfer = layui.transfer;
        var layer = layui.layer;
        var upload = layui.upload;
        var table = layui.table;

        //渲染第一实例
        table.render({
            elem: '#test'
            ,url:'/rpmPlan/findPlanTopic'
            ,where:{
                ctreateType:1
            }
            ,parseData: function(res){
                return {
                    "code":0,
                    "data": res.obj,
                };
            }
            ,cols: [[
                {type: 'numbers', title: '序号', align: 'center',width:200}
                ,{field:'planName', title:'申报计划名称',align: 'center'}
                ,{title:'操作', toolbar: '#barDemo',align: 'center',width:200}
            ]]
        });

        //监听单选按钮
        form.on('radio(ctreateType)', function(data){
            table.reload('test', {
                where: {
                    ctreateType:data.value
                }
            });
        });

        //监听第一实例行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;

            if(obj.event === 'enter'){
                layer.open({
                    type: 1,
                    title: '信息',
                    btn: ['返回'],
                    area: ['100%', '100%'],
                    offset: '0px',
                    content:'<div>' +
                        '       <a href="javascript:addGroup(' + data.planId + ',0);" class="layui-btn layui-btn-normal layui-btn-sm"  style=" margin: 10px 0px;  margin-left: 95%; ">添加</a>' +
                        '       <table class="layui-hide" id="test2" lay-filter="test2"></table>' +
                        '</div>',
                    success:function(){

                        //渲染第二实例表格
                        table.render({
                            elem: '#test2'
                            ,url:'/RpmResearchGrouping/findResearchGroupingMap'
                            ,where:{
                                planId:data.planId
                            }
                            ,parseData: function(res){
                                return {
                                    "code":0,
                                    "data": res.obj,
                                };
                            }
                            ,cols: [[
                                {title:'申报计划名称',align: 'center',templet:function(d){
                                        if (d.rpmPlanWithBLOBs != undefined){
                                            return d.rpmPlanWithBLOBs.planName;
                                        }
                                        return '';
                                    }}
                                ,{ title:'分组',align: 'center',templet:function(d){
                                        var researchName = d.researchName;
                                        //项目
                                        if (d.topicIds == ''){
                                            researchName += '-项目数量(0)';
                                        }else{
                                            var topicIdslength = d.topicIds.split(',').length-1;
                                            researchName += '-项目数量(' + topicIdslength + ')';
                                        }
                                        //专家
                                        if (d.epUserids == ''){
                                            researchName += '-专家人数(0)';
                                        }else{
                                            var epUseridslength = d.epUserids.split(',').length-1;
                                            researchName += '-专家人数(' + epUseridslength + ')';
                                        }
                                        return researchName;
                                    }}
                                ,{title:'操作' ,align: 'center',width:300 ,templet:function(d){
                                        return '<button type="button" class="layui-btn layui-btn-xs" lay-event="event1">编辑</button>' +
                                            '<button type="button" class="layui-btn layui-btn-normal  layui-btn-xs" lay-event="event2">添加项目</button>'+
                                            '<button type="button" class="layui-btn layui-btn-warm layui-btn-xs" lay-event="event3">专家分配</button>'+
                                            '<button type="button" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="event4">删除</button>';
                                    }}
                            ]]
                        });

                    }
                })
            }
        });

        //监听第二实例行工具事件
        table.on('tool(test2)', function(obj){
            var data = obj.data;

            switch (obj.event) {
                case 'event1':  //编辑
                    addGroup(data.planId,data.research);
                    break;
                case 'event2': //添加项目
                    TopicSelected(data.planId,data.research);
                    break;
                case 'event3': //专家分配
                    ExpertSelected(data.planId,data.research);
                    break;
                case 'event4': //删除
                    delGroup(data.rfgId);
                    break;
            }

        });

        //监听项目查询条件
        form.on('submit(searchLook)', function(data){
            var planid = $(data.elem).attr('planid');
            var field = data.field;
            transTable1(planid,field.School,field.category,field.RESEARCH);
            return false;
        });

        form.render();
    });


</script>
